package com.ocom.payconfig.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.MealPlanListRequest;
import com.ocom.common.request.payconfig.RemoveMealPlanRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.MealMenuService;
import com.ocom.payconfig.service.MealPlanService;
import com.ocom.payconfig.service.XfMerchantInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/2/20
 **/
@Slf4j
@RestController
public class MealPlanController {


	@Autowired
	private MealPlanService mealPlanService;

	@Autowired
	private MealMenuService mealMenuService;

	@Autowired
	private XfMerchantInfoService merchantInfoService;

	//11.3.1排餐计划列表（mealPlanList）
	@PostMapping(PayConfigUrl.Url_mealPlanList)
	public Result mealPlanList(@RequestBody MealPlanListRequest request) {
		List<EntityMealPlan> mealPlanList = mealPlanService.mealPlanList(request.getComId(), request);
		for (EntityMealPlan entityMealPlan:mealPlanList){
			EntityXfMerchantInfo entityXfMerchantInfo = merchantInfoService.GetMerchInfo(entityMealPlan.getComId(), Long.valueOf(entityMealPlan.getMerchId()));
			if(ObjectUtil.isNotEmpty(entityXfMerchantInfo)){
				entityMealPlan.setMerchName(entityXfMerchantInfo.getMerchName());
			}
		}
		return ResultUtil.success(mealPlanList);
	}

	//11.3.2删除排餐计划（removeMealPlan）
	@PostMapping(PayConfigUrl.Url_removeMealPlan)
	public Result removeMealPlan(@RequestBody RemoveMealPlanRequest request) {
		log.info("删除排餐计划   UPyK0Eu4    【{}】",request);
		mealPlanService.removeMealPlan(request.getComId(), request);
		mealMenuService.removeMealMenu(request.getComId(), request);
		return ResultUtil.success();
	}

	//11.3.3增加排餐计划（newMealPlan）
	@PostMapping(PayConfigUrl.Url_newMealPlan)
	public Result newMealPlan(@RequestBody EntityMealPlan entityMealPlan) {
		Integer total = mealPlanService.checkPlanName(entityMealPlan.getComId(), entityMealPlan, true);
		if (total > 0) {
			return ResultUtil.error(ResultEnum.Err_3071);
		}
		Boolean df = mealPlanService.newMealPlan(entityMealPlan.getComId(), entityMealPlan);
		if (!df) {
			return ResultUtil.error(ResultEnum.Err_4069);
		}
		return ResultUtil.success(entityMealPlan);
	}

	//11.3.4修改排餐计划（updateMealPlan）
	@PostMapping(PayConfigUrl.Url_updateMealPlan)
	public Result updateMealPlan(@RequestBody EntityMealPlan entityMealPlan) {
		Integer total = mealPlanService.checkPlanName(entityMealPlan.getComId(), entityMealPlan, false);
		if (total > 0) {
			return ResultUtil.error(ResultEnum.Err_4120);
		}
		Boolean df = mealPlanService.updateMealPlan(entityMealPlan.getComId(), entityMealPlan);
		if (!df) {
			return ResultUtil.error(ResultEnum.Err_3014);
		}
		return ResultUtil.success(entityMealPlan);
	}
}
